﻿using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media.Imaging;

using OpenCvSharp;

namespace Xc.Logics.CodeReaders.Models
{
    public class MatConverter : IValueConverter
    {
        public object Convert(object value, System.Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                if (value is Mat mat)
                {
                    BitmapImage bitmapImage = new BitmapImage();
                    bitmapImage.BeginInit();
                    var stream = new System.IO.MemoryStream();
                    mat.ToMemoryStream().CopyTo(stream);
                    bitmapImage.StreamSource = stream;
                    bitmapImage.EndInit();
                    return bitmapImage;
                }
                else
                {
                    return value;
                }
            }
            catch (System.Exception)
            {
                return value;
            }

        }

        public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture)
        {
            throw new System.NotImplementedException();
        }
    }
    public class MatFileConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string text)
            {
               return  Cv2.ImRead(text,ImreadModes.Color);
            }
            return null;
        }
    }
}
